package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.module.admin.entity.SysLeaveMessage;
import com.mars.module.admin.entity.SysReview;
import com.mars.module.admin.entity.SysSchool;
import com.mars.module.admin.mapper.SysSchoolMapper;
import com.mars.module.admin.service.ISysDictDataService;
import com.mars.module.system.entity.SysUser;
import com.mars.module.system.mapper.SysUserMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysStayApplicationRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysStayApplicationMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysStayApplication;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysStayApplicationService;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 住宿申请业务层处理
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysStayApplicationServiceImpl implements ISysStayApplicationService {

    private final SysStayApplicationMapper baseMapper;
    private final SysUserMapper userMapper;
    private final SysSchoolMapper schoolMapper;
    private final ISysDictDataService dataService;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysStayApplication add(SysStayApplicationRequest request) {
        SysStayApplication entity = SysStayApplication.builder().build();
        BeanUtils.copyProperties(request, entity);
        entity.setStatus("0");
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysStayApplicationRequest request) {
        SysStayApplication entity = SysStayApplication.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysStayApplication getById(Long id) {
        SysStayApplication e = baseMapper.selectById(id);
        SysUser sysUser = userMapper.selectById(e.getUserId());
        e.setUserName(sysUser == null ? "" : sysUser.getUserName());
        SysSchool sysSchool = schoolMapper.selectById(e.getSchoolId());
        e.setSchoolName(sysSchool == null ? "" : sysSchool.getName());
        return e;
    }

    @Override
    public PageInfo<SysStayApplication> pageList(SysStayApplicationRequest request) {
        Page<SysStayApplication> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysStayApplication> query = this.buildWrapper(request);
        IPage<SysStayApplication> pageRecord = baseMapper.selectPage(page, query);
        List<SysStayApplication> records = pageRecord.getRecords();
        List<SysStayApplication> recordsNew = new ArrayList<>();
        for (SysStayApplication e : records) {
            SysUser sysUser = userMapper.selectById(e.getUserId());
            e.setUserName(sysUser == null ? "" : sysUser.getUserName());
            SysSchool sysSchool = schoolMapper.selectById(e.getSchoolId());
            e.setSchoolName(sysSchool == null ? "" : sysSchool.getName());
            e.setStatusName(dataService.getDictDataById("approve_status", e.getStatus()));
            recordsNew.add(e);
        }
        if (StringUtils.isNotBlank(request.getSchoolName())) {
            recordsNew = recordsNew.stream().filter(e -> e.getSchoolName().contains(request.getSchoolName())).collect(Collectors.toList());
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysStayApplication> list(SysStayApplicationRequest request) {
        LambdaQueryWrapper<SysStayApplication> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    @Override
    public Map<String, Long> stayApplyCount() {
        List<SysStayApplication> stayList = baseMapper.selectList(new QueryWrapper<>());
        stayList.stream().forEach(
                e -> {
                    e.setCreateTimeDate(e.getCreateTime().format(DateTimeFormatter.ofPattern("yyyy-MM")));
                }
        );
        Map<String, Long> collect = stayList.stream().collect(Collectors.groupingBy(SysStayApplication::getCreateTimeDate,
                Collectors.counting()));
        return collect;
    }

    private LambdaQueryWrapper<SysStayApplication> buildWrapper(SysStayApplicationRequest param) {
        LambdaQueryWrapper<SysStayApplication> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(param.getFloorNumber())) {
            query.like(SysStayApplication::getFloorNumber, param.getFloorNumber());
        }
        if (StringUtils.isNotBlank(param.getApplyDate())) {
            query.like(SysStayApplication::getApplyDate, param.getApplyDate());
        }
        if (StringUtils.isNotBlank(param.getStatus())) {
            query.like(SysStayApplication::getStatus, param.getStatus());
        }
        if (param.getUserId() != null) {
            query.like(SysStayApplication::getUserId, param.getUserId());
        }
        return query;
    }

}
